<?php
require_once dirname(__FILE__).'/UserService.php';

session_start();
$user = $_SESSION['user'];

if (isset($user)) {
    echo 'success';
    return;
}

if (isset($_POST['loginname']) && isset($_POST['password'])) {
    $loginname = $_POST['loginname'];
    $password = $_POST['password'];
    $service = new UserService();
    $st = $service->auth($loginname, $password);
    switch ($st) {
        case 0:
            $user = $service->getUserByLoginname($loginname);
            $_SESSION['user'] = $user;
            echo 'success';
            break;
        case 1:
            echo 'error_password';
            break;
        case 2:
            echo 'error_status';
            break;
        case 4:
            echo 'expired';
            break;
        default:
            echo 'error';
            break;
    }
} else {
    echo 'error';
}
return;
?>